# import cv2
#
# # 读取图像
# img = cv2.imread("tk.jpeg", 0)  # 0表示读取为灰度图像
#
# # 全局直方图均衡化
# imgEqu = cv2.equalizeHist(img)
#
# # 创建CLAHE对象
# clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(3, 3))
# # 自适应局部直方图均衡化
# imgLocalEqu = clahe.apply(img)
#
# # 设置显示的图像大小
# width = 400
# height = 400
# dim = (width, height)
#
# # 调整图像大小
# img_resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
# imgEqu_resized = cv2.resize(imgEqu, dim, interpolation=cv2.INTER_AREA)
# imgLocalEqu_resized = cv2.resize(imgLocalEqu, dim, interpolation=cv2.INTER_AREA)
#
# # 显示图像
# cv2.imshow('Original Image', img_resized)
# cv2.imshow('Global Histogram Equalization', imgEqu_resized)
# cv2.imshow('Local Histogram Equalization', imgLocalEqu_resized)
#
# # 等待按键后关闭所有窗口
# cv2.waitKey(0)
# cv2.destroyAllWindows()



import cv2
import numpy as np

# 读取灰度图像
img = cv2.imread("tk.jpeg", 0)

# 创建参数组合矩阵
params = [
    # clipLimit, tileGridSize
    (1.0, (8,8)),    # 低对比度限制，大网格
    (2.0, (8,8)),    # 中等对比度限制，大网格
    (3.0, (8,8)),    # 高对比度限制，大网格
    (3.0, (3,3)),    # 高对比度限制，小网格
    (5.0, (3,3)),    # 超高对比度限制，小网格
]

# 生成对比结果
results = []
for clip, grid in params:
    clahe = cv2.createCLAHE(clipLimit=clip, tileGridSize=grid)
    enhanced = clahe.apply(img)
    results.append(enhanced)

# 设置统一显示尺寸
display_size = (300, 300)

# 创建对比面板
comparison = np.vstack([
    np.hstack([cv2.resize(img, display_size)] +
              [cv2.resize(r, display_size) for r in results[:2]]),
    np.hstack([cv2.resize(r, display_size) for r in results[2:]])
])

# 添加参数标注
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(comparison, "Original", (10,30), font, 0.7, 255, 2)
for i, (clip, grid) in enumerate(params):
    x = 310 if i<2 else (i-2)*300 + 10
    y = 30 if i<2 else 330
    cv2.putText(comparison,
                f"CL={clip} GS={grid}x{grid}",
                (x, y), font, 0.6, 255, 1)

# 显示对比结果
cv2.imshow("CLAHE Parameter Comparison (Rows: Original | Enhanced)", comparison)
cv2.waitKey(0)
cv2.destroyAllWindows()